/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-11-20 10:09
**/
package wsgroup


import (
	"ydd-im/service/api/services/wsservice/account"
	"ydd-im/service/api/services/wsservice/client"
	"ydd-im/service/api/services/wsservice/group"
	//"ydd-im/service/api/services/wsservice/client"
	"ydd-im/service/api/tools/bind"
	"ydd-im/service/api/tools/response"
	"github.com/gin-gonic/gin"
	//"github.com/go-playground/validator/v10"
)
// 将当前用户拉入群

type Controller struct {
}


type inputData struct {
	ToUserId  int `json:"touserid" form:"touserid" binding:"required" `
	GroupId int `json:"groupid" form:"groupid" `

}
//  绑定用户入群
func (r *Controller) Bindusertogroup(c *gin.Context) {
	var inputData inputData
	_, err := bind.Bind(&inputData, c)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}

	// 获取token查询的用户信息
	_,err = account.GetMiddleUserInfo(c)
	if  err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	// 判断添加用户是否在群内
	ok,err := group.GetGroupUserInfo(inputData.ToUserId,inputData.GroupId)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:  response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	if !ok { // 用户不在群内，添加群
		err = group.AddGroupContact(inputData.ToUserId,inputData.GroupId)
		if err != nil {
			response.ResponseJsonFail(c, response.Response{
				Code:  response.ErrorCode,
				Message: err.Error(),
			})
			return
		}
	}
	client.AddUserToGroup(inputData.GroupId, inputData.ToUserId)
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",

	})
	return
}

// 获取签名相关的所有用户
func (r *Controller) GetAllGroupList(c *gin.Context) {
	// 获取appkey查询的用户信息
	//appkey := c.Request.Header.Get("appkey")
	userInfo, err := account.GetMiddleUserInfo(c)
	if  err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	UserList,err := group.GetAllGroupList(userInfo.Sid,userInfo.Id)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",
		Data: UserList,
	})
	return
}
/**
 * 获取用户所属群
 * @access  public
 * @param   string     token  用户token
 * @return  mix
 */
func (r *Controller) GetAllJoinGroupList(c *gin.Context) {
	// 获取appkey查询的用户信息
	//appkey := c.Request.Header.Get("appkey")
	userInfo, err := account.GetMiddleUserInfo(c)
	if  err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: "用户不存在，请先登录系统",
		})
		return
	}
	GroupList,err := group.GetAllJoinGroupList(userInfo.Sid,userInfo.Id)
	if err != nil {
		response.ResponseJsonFail(c, response.Response{
			Code:    response.ErrorCode,
			Message: err.Error(),
		})
		return
	}
	response.ResponseJsonOk(c, response.Response{
		Code:  response.SuccessCode,
		Message: "success",
		Data: GroupList,
	})
	return
}